//
//  CSYEmoticonKeyboardView.swift
//  CSYEmoticonKeyboard__Test
//
//  Created by 姚彦兆 on 15/11/21.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

class CSYEmoticonKeyboardView: UIView {
    
    lazy var packageArray : [CSYEmoticonPackageModel] = CSYEmoticonManager.manager.packageArray
    
    //闭包传递点击事件
    
    var returnBlock : ((model : CSYEmoticonModel) -> ())?
    
    @objc private func clickToolBarButton(barButtonItem : UIBarButtonItem){
    
        let indexPath : NSIndexPath = NSIndexPath(forItem: 0, inSection: barButtonItem.tag)
    
        keyboardCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
    
    }
    
    init(returnBlock : (CSYEmoticonModel) -> ()){
        
        self.returnBlock = returnBlock
        
        super.init(frame: CGRectMake(0, 0, screen_W, 220))
        
        setUpUI()
        
    }

    private func setUpUI(){
    
        addSubview(toolBar)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            make.left.right.bottom.equalTo(self)
            
            make.height.equalTo(toolBar_H)
            
        }
        
        addSubview(keyboardCollectionView)
        
        keyboardCollectionView.snp_makeConstraints { (make) -> Void in
            
            make.left.right.top.equalTo(self)
            
            make.bottom.equalTo(toolBar.snp_top)
            
        }
        
        setToolBar()
    
    }
    
    private func setToolBar(){

        toolBar.tintColor = UIColor.lightGrayColor()

        var items : [UIBarButtonItem] = [UIBarButtonItem]()
        
        var index : NSInteger = 0
        
        for package in packageArray{
        
            let btn : UIBarButtonItem = UIBarButtonItem(title: package.group_name_cn, style: UIBarButtonItemStyle.Plain, target: self, action: "clickToolBarButton:")
            
            let flexible : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        
            items.append(btn)
            
            items.last?.tag = index++
            
            items.append(flexible)
        
        }
        
        items.removeLast()

        toolBar.items = items
    
    }

    lazy var keyboardCollectionView : UICollectionView = {
    
        let layout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = 0
        
        layout.minimumInteritemSpacing = 0

        layout.scrollDirection = .Horizontal
        
        layout.itemSize = CGSizeMake(emoticon_W,emoticon_W)
        
        let viewMargin : CGFloat = ((220 - toolBar_H) - 3 * emoticon_W) / 4
    
        let collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
    
        collectionView.backgroundColor = UIColor.whiteColor()
        
        collectionView.contentInset = UIEdgeInsetsMake(viewMargin, 0, viewMargin, 0)
        
        collectionView.pagingEnabled = true

        collectionView.showsVerticalScrollIndicator = false
        
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.delegate = self
        
        collectionView.dataSource = self
        
        collectionView.registerClass(KeyboardCollectionViewCell.self, forCellWithReuseIdentifier: kCellID)
        
        return collectionView
    
    }()
    
    
    lazy var toolBar : UIToolbar = UIToolbar()
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension CSYEmoticonKeyboardView : UICollectionViewDataSource,UICollectionViewDelegate{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return packageArray.count
        
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return packageArray[section].emoticonModelArray.count
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kCellID, forIndexPath: indexPath) as! KeyboardCollectionViewCell
        
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.clearColor() : UIColor.clearColor()
        
        let model : CSYEmoticonModel = packageArray[indexPath.section].emoticonModelArray[indexPath.row]
        
        cell.model = model
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let model = packageArray[indexPath.section].emoticonModelArray[indexPath.item]
        
        returnBlock?(model: model)
        
    }
    
    
}

class KeyboardCollectionViewCell : UICollectionViewCell {

    var model : CSYEmoticonModel? {
    
        didSet{
    
            emoticonButton.setImage(UIImage(contentsOfFile: model?.pngPath ?? ""), forState: UIControlState.Normal)
            
            emoticonButton.setTitle(model?.defaultEmoticon, forState: UIControlState.Normal)
            
            if let em = model where em.isRemove{
            
                emoticonButton.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
            
                emoticonButton.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Normal)
                
            }
            
        }
    
    }
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        contentView.addSubview(emoticonButton)
        
    }

    lazy var emoticonButton : UIButton = {
    
        let btn : UIButton = UIButton()
    
        btn.backgroundColor = UIColor.clearColor()

        btn.userInteractionEnabled = false
        
        btn.frame = CGRectInset(self.bounds, 4, 4)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(32)
        
        return btn
        
    }()
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}
































